package com.mindao.app.ued.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mindao.app.common.spring.controller.BaseBaseController;
import com.mindao.app.common.spring.service.util.JsonResult;

/**
/* 版权所有： 广州敏道科技有限公司
/*
/* 功能描述：转盘大抽奖
 * 
 * 
 * @author liguocai
 *
 */
@Controller
@RequestMapping("/ued/prize")
public class PrizeController extends BaseBaseController{
	private final Logger log = LoggerFactory.getLogger(PrizeController.class);
	
	@RequestMapping
	public String draw( HttpServletRequest request,HttpServletResponse response,HttpSession session,Model rhs){
		return super.rtnDefault(request, response, session, rhs);
	}
	
	/**
	 * 进行抽奖
	 * @param request
	 * @param response
	 * @param session
	 * @param rhs
	 * @return
	 */
	@RequestMapping
	@ResponseBody
	public JsonResult doDraw( HttpServletRequest request,HttpServletResponse response,HttpSession session,Model rhs){
		 //准备奖池,中奖率相加应该小于等于1
		Prize p1=new Prize(1,"100元",BigDecimal.valueOf(0.01));
		Prize p2=new Prize(2,"10元",BigDecimal.valueOf(0.1));
		Prize p3=new Prize(3,"1元",BigDecimal.valueOf(0.3));
 
		
		List<Prize> prizeList=new ArrayList<Prize>();
		prizeList.add(p1);
		prizeList.add(p2);
		prizeList.add(p3);
		
		JsonResult jr=new JsonResult(JsonResult.SUCCESS,"ok");
		BigDecimal random=BigDecimal.valueOf(Math.random());
		System.out.println("抽中数字:"+random.doubleValue());
		BigDecimal curPrizeLimit=BigDecimal.valueOf(0);
		for (int i = 0; i < prizeList.size(); i++) {
			Prize prize=prizeList.get(i);
			curPrizeLimit=curPrizeLimit.add(prize.getHitPer());
			if (random.compareTo(curPrizeLimit)==-1){
				jr.setData(prize);
				break;
			}
		}
		return jr;
	}
	
	 class Prize {
		private Integer myLevel; //奖品等级
		private String name;    //奖品名称
		//private Integer count;    //奖品数量，有数量时，每次抽奖后要保存抽奖结果，奖池需要重新计算，这里自作转盘抽奖演示，假设数量不限
		private BigDecimal hitPer;    //中奖率(百分数)
		
		public Prize(Integer myLevel, String name, BigDecimal hitPer) {
			super();
			this.myLevel = myLevel;
			this.name = name;
			this.hitPer = hitPer;
		}
		public Integer getMyLevel() {
			return myLevel;
		}
		public void setMyLevel(Integer myLevel) {
			this.myLevel = myLevel;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public BigDecimal getHitPer() {
			return hitPer;
		}
		public void setHitPer(BigDecimal hitPer) {
			this.hitPer = hitPer;
		}
		  
	 }	
	 
	 public static void main(String[] args) {
		int times=100000;
		int times1=0;
		int times2=0;
		int times3=0;
		
		BigDecimal curPrizeLimit1=BigDecimal.valueOf(0.01);
		BigDecimal curPrizeLimit2=BigDecimal.valueOf(0.11);
		BigDecimal curPrizeLimit3=BigDecimal.valueOf(0.25);
		for (int i = 0; i < times; i++) {
			BigDecimal random=BigDecimal.valueOf(Math.random());
			if (random.compareTo(curPrizeLimit1)==-1){
				System.out.println("第"+1+"次结果"+random+",一等奖");
				times1++;
			}else if (random.compareTo(curPrizeLimit2)==-1){
				System.out.println("第"+1+"次结果"+random+",二等奖");
				times2++;
			}else if (random.compareTo(curPrizeLimit3)==-1){	
				System.out.println("第"+1+"次结果"+random+",三等奖");
				times3++;
			}else{
				System.out.println("第"+1+"次结果"+random);
			}
		}
		System.out.printf("一个共%d 次，其中一等奖%d ,二等奖%d ,三等奖%d  %n",times,times1,times2,times3);
	}
	 
 
}
